/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchResult;

import constants.UiConstants;
import exceptions.NoMatchFoundException;
import ui.messages.MessageBoxes;
import utils.FileUtils;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Class for managing events on SearchResultForm
 *
 * @author rymek
 *         User: rymek
 *         Date: 02.08.11
 *         Time: 12:17
 */
class SearchResultFormActionListener implements ActionListener {

    private final SearchResultForm searchResultForm;        // parent form

    /**
     * Constructor.
     * Also sets reference to parent form
     *
     * @param searchResultForm parent form
     */
    public SearchResultFormActionListener(SearchResultForm searchResultForm) {
        this.searchResultForm = searchResultForm;
    }

    /**
     * Performs actions according to specific button press
     *
     * @param e the event
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        try {
            if (e.getSource() == this.searchResultForm.getSaveRequest()) this.saveRequest();
            if (e.getSource() == this.searchResultForm.getSaveResponse()) this.saveResponse();
            if (e.getSource() == this.searchResultForm.getMerge()) this.mergeMessages();
            if (e.getSource() == this.searchResultForm.getFindIn()) this.findIn();

        } catch (IOException error) {
            MessageBoxes.genericErrorMessage(error);
        }
    }

    /**
     * Saves request into user specified file
     *
     * @throws IOException when IO access occurs
     */
    private void saveRequest() throws IOException {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setFileFilter(new FileNameExtensionFilter(UiConstants.XML_FILE_TYPE, UiConstants.XML_EXTENSION));

        fileChooser.setSelectedFile(this.searchResultForm.getResultRequest().getXmlResultFile());

        int returnVal = fileChooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            FileUtils.copyFile(this.searchResultForm.getResultRequest(), fileChooser.getSelectedFile());
        }
    }

    /**
     * Saves response into user specified file
     *
     * @throws IOException when IO access occurs
     */
    private void saveResponse() throws IOException {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setFileFilter(new FileNameExtensionFilter(UiConstants.XML_FILE_TYPE, UiConstants.XML_EXTENSION));

        fileChooser.setSelectedFile(this.searchResultForm.getResultResponse().getXmlResultFile());

        int returnVal = fileChooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            FileUtils.copyFile(this.searchResultForm.getResultResponse(), fileChooser.getSelectedFile());
        }
    }

    /**
     * Merges request and response to one file
     *
     * @throws IOException when IO error occurs
     */
    private void mergeMessages() throws IOException {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setFileFilter(new FileNameExtensionFilter(UiConstants.XML_FILE_TYPE, UiConstants.XML_EXTENSION));

        fileChooser.setSelectedFile(new File(this.searchResultForm.getResultRequest().getXmlResultFile().getName().replace("REQ", "REQ-RPLY")));

        int returnVal = fileChooser.showSaveDialog(null);

        String mergedXml = this.searchResultForm.getResultRequest().getXmlString() + "\n\n\n" + this.searchResultForm.getResultResponse().getXmlString();

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            FileUtils.mergeMessagesToFile(fileChooser.getSelectedFile(), mergedXml);
        }
    }

    /**
     * Performs find on specific result field
     */
    private void findIn() {

        try {

            this.searchResultForm.findIn();

        } catch (NoMatchFoundException error) {
            MessageBoxes.noMatchingStringFound(error);
        }

    }
}
